/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package todomanager.io;

/**
 *
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import todomanager.data.ITask;
import todomanager.data.StandardTask;

/**
 * A XML reader that reads XML data returning it to an ArrayList.
 *
 * @author jonasbirgersson
 * @author Shirin
 */
public class TodoXMLReader implements ITodoImporter {

	BufferedReader br;

	public TodoXMLReader(BufferedReader br) {
		this.br = br;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see todo.io.ItodoImporter#importTodo()
	 */
	@Override
	public ArrayList<ITask> importTodo() throws IOException {
		ArrayList<ITask> tasks = new ArrayList<ITask>();
		SAXBuilder builder = new SAXBuilder();
		try {
			Document doc = builder.build(br);
			Iterator<Element> taskIterator = doc.getRootElement().getChildren().iterator();
			while(taskIterator.hasNext()){
				Element current = taskIterator.next();
				String name = current.getChildText("name");
				String description =current.getChildText("description");
				String dueDate = current.getChildText("dueDate");
				String category = current.getChildText("category");
				int priority = (Integer.parseInt(current.getChildText("priority")));
				int progress = (Integer.parseInt(current.getChildText("progress")));				
				boolean done = (current.getChildText("done").compareTo("true")==0);
				tasks.add(new StandardTask(name,description,dueDate,category,priority,progress,done));
			}
		} catch (JDOMException e) {
			//throw new IOException(e.getMessage());
		}
		return tasks;
	}
	
	public ArrayList importName() throws IOException {
		ArrayList Date = new ArrayList();
		SAXBuilder builder = new SAXBuilder();
		try {
			Document doc = builder.build(br);
			Iterator<Element> taskIterator = doc.getRootElement().getChildren().iterator();
			while(taskIterator.hasNext()){
				Element current = taskIterator.next();
				String name = current.getChildText("name");
				Date.add(name);
			}
		} catch (JDOMException e) {
			//throw new IOException(e.getMessage());
		}
		return Date;
	}
	
	public ArrayList importDescription() throws IOException {
		ArrayList Date = new ArrayList();
		SAXBuilder builder = new SAXBuilder();
		try {
			Document doc = builder.build(br);
			Iterator<Element> taskIterator = doc.getRootElement().getChildren().iterator();
			while(taskIterator.hasNext()){
				Element current = taskIterator.next();
				String description =current.getChildText("description");
				Date.add(description);
			}
		} catch (JDOMException e) {
			//throw new IOException(e.getMessage());
		}
		return Date;
	}
	
	public ArrayList importDate() throws IOException {
		ArrayList Date = new ArrayList();
		SAXBuilder builder = new SAXBuilder();
		try {
			Document doc = builder.build(br);
			Iterator<Element> taskIterator = doc.getRootElement().getChildren().iterator();
			while(taskIterator.hasNext()){
				Element current = taskIterator.next();
				String dueDate = current.getChildText("dueDate");
				Date.add(dueDate);
			}
		} catch (JDOMException e) {
			//throw new IOException(e.getMessage());
		}
		return Date;
	}
	
	public ArrayList importCategory() throws IOException {
		ArrayList Date = new ArrayList();
		SAXBuilder builder = new SAXBuilder();
		try {
			Document doc = builder.build(br);
			Iterator<Element> taskIterator = doc.getRootElement().getChildren().iterator();
			while(taskIterator.hasNext()){
				Element current = taskIterator.next();
				String category = current.getChildText("category");
				Date.add(category);
			}
		} catch (JDOMException e) {
			//throw new IOException(e.getMessage());
		}
		return Date;
	}
	
	public ArrayList importPriority() throws IOException {
		ArrayList Date = new ArrayList();
		SAXBuilder builder = new SAXBuilder();
		try {
			Document doc = builder.build(br);
			Iterator<Element> taskIterator = doc.getRootElement().getChildren().iterator();
			while(taskIterator.hasNext()){
				Element current = taskIterator.next();
				int priority = (Integer.parseInt(current.getChildText("priority")));
				Date.add(priority);
			}
		} catch (JDOMException e) {
			//throw new IOException(e.getMessage());
		}
		return Date;
	}
	
	public ArrayList importProgress() throws IOException {
		ArrayList Date = new ArrayList();
		SAXBuilder builder = new SAXBuilder();
		try {
			Document doc = builder.build(br);
			Iterator<Element> taskIterator = doc.getRootElement().getChildren().iterator();
			while(taskIterator.hasNext()){
				Element current = taskIterator.next();
				int progress = (Integer.parseInt(current.getChildText("progress")));
				Date.add(progress);
			}
		} catch (JDOMException e) {
			//throw new IOException(e.getMessage());
		}
		return Date;
	}

	@Override
	public ArrayList importList() throws IOException {
		// TODO Auto-generated method stub
		return null;
	}
}
